﻿// See https://aka.ms/new-console-template for more information
using System.IO.MemoryMappedFiles;
using System.Runtime.InteropServices;
using System.Text;

public class Program
{
    [DllImport("kernel32.dll", SetLastError = true)]
    public static extern IntPtr CreateFileMapping(IntPtr hFile, IntPtr lpAttributes, uint flProtect, uint dwMaximumSizeHigh, uint dwMaximumSizeLow, string lpName);

    [DllImport("kernel32.dll", SetLastError = true)]
    public static extern IntPtr MapViewOfFile(IntPtr hFileMappingObject, uint dwDesiredAccess, uint dwFileOffsetHigh, uint dwFileOffsetLow, uint dwNumberOfBytesToMap);

    [DllImport("kernel32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool UnmapViewOfFile(IntPtr lpBaseAddress);

    [DllImport("kernel32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool CloseHandle(IntPtr hObject);

    static void Main()
    {
        const string mapName = "MySharedMemory";
        const int bufferSize = 1024; // 共享内存的大小

        using (MemoryMappedFile mmf = MemoryMappedFile.CreateOrOpen(mapName, bufferSize))
        using (MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor())
        {
            string message = "Hello from Process A";
            byte[] buffer = Encoding.UTF8.GetBytes(message);
            
            accessor.WriteArray(0, buffer, 0, buffer.Length);
            Console.WriteLine(message);
            Thread.Sleep(10000);

            byte[] buffer2=new byte[bufferSize];
            accessor.ReadArray(0, buffer2, 0, bufferSize);
            message= Encoding.UTF8.GetString(buffer2);
            Console.WriteLine(message);
        }
        Console.ReadLine();
    }
    //static void Main(string[] args)
    //{
    //    Console.WriteLine("Hello, World!");
    //    Prg prg = new Prg();
    //    prg.Do("Hello, World!");
    //    Console.ReadLine();
    //}
}



//// 启动项目2的可执行文件
//ProcessStartInfo startInfo = new ProcessStartInfo
//{
//    FileName = "E:\\MyGit\\funny-arts.-wpf\\ConsoleServer\\bin\\Debug\\net6.0\\ConsoleServer.exe", // 替换为项目2的可执行文件路径
//    UseShellExecute = false,
//    RedirectStandardOutput = true
//};

//using (Process process = new Process())
//{
//    process.StartInfo = startInfo;
//    process.Start();

//    // 向项目2发送请求
//    using (NamedPipeClientStream clientPipe = new NamedPipeClientStream(".", "MyNamedPipe", PipeDirection.InOut))
//    {
//        Console.WriteLine("Connecting to project2...");
//        clientPipe.Connect();

//        using (StreamWriter writer = new StreamWriter(clientPipe))
//        using (StreamReader reader = new StreamReader(clientPipe))
//        {
//            string request = "Request from project1";
//            writer.WriteLine(request);
//            writer.Flush();

//            process.WaitForExit(); // 等待项目2执行完成

//            // 读取项目2的响应
//            string response = reader.ReadLine();
//            Console.WriteLine($"Received response from project2: {response}");
//        }
//    }

//}
